package com.valveinspectionsys.httpserver;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;


/**
 * 批量上传管理
 * @author King
 *
 */
public class UploadManager {
    
    
       private List<UploadItem> list;
       
       
         // 上传地址
   	private String url;
   	// 上传任务线程池
   	private ExecutorService executorService;
   	// 应用程序上下文
   	private Context context;
   	
   	private Handler uploadhand;
        
   	private SQLiteDatabase db;
   	
   	private int index=0;
   	
   	private boolean isStop=false;
  
   	public UploadManager(Context context,Handler uploadhand,SQLiteDatabase db){
   	    this.context=context;
   	    this.uploadhand=uploadhand;
            this.db=db;
   	}
   	
   	public void addTask(String url, UploadItem items) {
		this.url = url;
		
		items.setUrl(url);
		executorService.submit(new UploadThread(items));
	}

   	//停止没做的任务或正在执行的任务
	public void stop() {
	       if(!isStop){
		   executorService.shutdownNow();
		   isStop=true;
	       }
		
	}

	public void start() {
		// 创建线程池
		executorService = Executors.newFixedThreadPool(5);
	}

	public boolean isStop() {
		return executorService.isShutdown();
	}
	
	class UploadThread implements Runnable {
		private UploadItem items;

		public UploadThread(UploadItem items) {
		    this.items=items;
		}

		@Override
		public void run() {
		        
			String response=UploadHttpClient.uploadSubmit(items.getUrl(), items);	
			JSONObject  json=null;
			try {
			      json=new JSONObject(response);
			} catch (JSONException e) {
			    if(!isStop){				   
				    Message msg=uploadhand.obtainMessage();
				    msg.what=0;
				   uploadhand.sendMessage(msg);
			    }
			
			    e.printStackTrace();
			}
			if(response.equals("error")){
			    if(!isStop){
				   
				    Message msg=uploadhand.obtainMessage();
				    msg.what=0;
				   uploadhand.sendMessage(msg);
			    }
			
			} else
			    try {
				if(json.getString("code").equals("1")){
				    Message msg=uploadhand.obtainMessage();
				    msg.what=1;
				    List<UpdateVo> list=items.getUp();
				    for(int i=0;i<list.size();i++){
					UpdateVo up=list.get(i);
				      int k=	db.update(up.getTablename(), up.getValues(),up.getWhereClause(),null);			     
				    }
				    index++;
				  
				    uploadhand.sendMessage(msg);
				}else if(json.getString("code").equals("0")){
				    if(!isStop){				   
					    Message msg=uploadhand.obtainMessage();
					    msg.what=0;
					   uploadhand.sendMessage(msg);
				    }
				}
			    } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			    }
		    }
		}

	public List<UploadItem> getList() {
	    return list;
	}

	public void setList(List<UploadItem> list) {
	    this.list = list;
	}

	public int getIndex() {
	    return index;
	}

	public void setIndex(int index) {
	    this.index = index;
	}
	

	
   	
}
